package com.loser.module.base;

import io.github.loserya.core.sdk.mapper.BaseMapper;
import io.github.loserya.global.BaseMapperContext;
import io.github.loserya.utils.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.List;

@Component
public class BaseServiceImpl implements BaseService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public boolean save(Base base) {
        BaseMapper<Serializable, Base> mapper = BaseMapperContext.getMapper(Base.class);
        MongoTemplate template = mapper.getTemplate();
        if (!template.equals(mongoTemplate)) {
            throw ExceptionUtils.mpe("mongoTemplate 不一致");
        }
        mongoTemplate.save(base);
        return true;
    }

    @Override
    public List<Base> list() {
        return mongoTemplate.findAll(Base.class);
    }

}
